# 字典的创建
d1 = {
    'name': 'J.R',
    1: 2,
    ('爱好',): ['drink', 'smoke', 'play']
}
print(d1)  # {'name': 'J.R', 1: 2, ('爱好',): ['drink', 'smoke', 'play']}
print(type(d1))  # <class 'dict'>

d2 = {}
print(d2)
d3 = dict()
print(d3)
d4 = dict(a = 1, b = 2)
print(d4)
d5 = dict([('a', 1), ('b', 2)])
print(d5)
d6 = dict({'a': 1, 'b': 2})
print(d6)

# 字典的操作
d7 = {1: 'hello'}
d7['height'] = 170  # 添加
print(d7['height']) # 获取值
d7[1] = 'world'  # 修改
print(1 in d7)  # 成员：针对键

# 字典的遍历
for i in d7:
    print(i, d7[i])

for k, v in d7.items():
    print(k, v)

for k in d7.keys():
    print(k)

for v in d7.values():
    print(v)

# 字典的常用方法
d8 = {1: 2, 'hello': 'world', 'height': 56}
d8.pop(1)
print(d8)  # {'hello': 'world', 'height': 56}
print(d8.get('hello'))  # world
d8.update({'age' : 19})
print(d8)  # {'hello': 'world', 'height': 56, 'age': 19}
d9 = d8.copy()
print(d9)  # {'hello': 'world', 'height': 56, 'age': 19}